package com.example.demo2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements Uservice {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void aa(){

        RedisConnection conn=redisTemplate.getConnectionFactory().getConnection();
       System.out.println(conn);

        redisTemplate.opsForList().leftPush("alist","张三");
        redisTemplate.opsForList().leftPush("alist","张三2");
        redisTemplate.opsForList().leftPush("alist","张三3");

        redisTemplate.opsForValue().set("MM","wodemeimei",10L, TimeUnit.SECONDS);

/*



        boolean isExists=redisTemplate.hasKey("MM");
        if(isExists){
            System.out.println("确实存在");
        }
        else{
            System.out.println("不存在");
        }
        Set<String> keys=redisTemplate.keys("AMB*");

        System.out.println(keys+"-------当我不存在吗？");
        for(String key :keys){
            System.out.println("oh,shit"+key);
        }

*/
        Set<String> keys2=redisTemplate.keys("MBA*");
        System.out.println(keys2+"----2222-------当我不存在吗？");
        for(String key :keys2){
            System.out.println("oh,shit2--------"+key);
        }

        System.out.println();

    }

    public String getMM(){
        int i=10;
        while(true){
            i--;
            if(i<0){
                break;
            }
            String s1=redisTemplate.opsForList().rightPop("alist");
            System.out.println("wocccc---------------》"+s1);
        }

        return "success";
    }


    @Override
    @Cacheable(value = "AMB", key = "#serialId+'_'+#opt")
    public String testCache(String serialId,String opt){
        try{
            System.out.println(serialId+"----等一等 耐心啊----"+opt);
            Thread.sleep(5000);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return "AMB";
    }

    @Override
    @CacheEvict(value = "AMB", allEntries = true)
    public void clearCscoocuchaBList() {

    }

}
